package com.hup.sokoban.view.mapDesigner;

import com.hup.sokoban.constant.MapConstant.ElementType;
import com.hup.utils.swing.customView.ImageButton;
import lombok.Getter;
import lombok.extern.log4j.Log4j2;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionListener;
import java.util.ArrayList;

/**
 * 获取[要绘制的元素]的面板
 *
 * @author hugan
 * @date 2020/5/1
 */
@Log4j2
public class MapElementPanel extends JPanel {
    private JPanel root;
    private ImageButton btnEmpty;
    private ImageButton btnWall;
    private ImageButton btnCage;
    private ImageButton btnAli;
    private ImageButton btnBird;

    private ArrayList<ImageButton> lstButton;
    /**
     * 当前的元素
     */
    @Getter
    private ElementType elementType;

    public MapElementPanel() {
        $$$setupUI$$$();
        lstButton = new ArrayList<>();
        for (Component c : getComponents()) {
            if (c instanceof ImageButton) {
                ImageButton cb = (ImageButton) c;
                cb.setCheckbox(true);
                cb.setAlwaysColored(true);
                cb.setPadding(3);
                lstButton.add(cb);
            }
        }
        initListener();
        //初始化ElementType
        btnEmpty.setSelected(true);
        updateElementType();
    }

    private void initListener() {
        ActionListener al = e -> {
            //处理按下事件,最后肯定会选中一种元素
            Object source = e.getSource();
            if (btnEmpty.equals(source) || btnWall.equals(source)) {
                for (ImageButton c : lstButton) c.setSelected(c.equals(source));
            } else if (btnCage.equals(source)) {
                btnEmpty.setSelected(false);
                btnWall.setSelected(false);
                boolean ali = btnAli.isSelected();
                boolean bird = btnBird.isSelected();
                if (!ali && !bird) {
                    //无阿狸无小鸟时,点击笼子,笼子必选
                    btnCage.setSelected(true);
                }
            } else if (btnAli.equals(source) || btnBird.equals(source)) {
                btnEmpty.setSelected(false);
                btnWall.setSelected(false);

                if (!btnCage.isSelected() || ((ImageButton) source).isSelected()) {
                    //当前元素必选,另一个(阿狸或小鸟)必不选
                    //进入条件有点诡异,这里文字解析难,需要结合实际操作才能理解
                    btnAli.setSelected(btnAli.equals(source));
                    btnBird.setSelected(btnBird.equals(source));
                }
                //else 否则就不改变当前状态
            }
            updateElementType();
        };
        for (ImageButton c : lstButton) c.setActionListener(al);
    }

    /**
     * 调用前,必须保证[复选框]的选中状态是有效的
     */
    private void updateElementType() {
        if (btnEmpty.isSelected()) {
            elementType = ElementType.EMPTY;
        } else if (btnWall.isSelected()) {
            elementType = ElementType.WALL;
        } else {
            boolean cage = btnCage.isSelected();
            boolean ali = btnAli.isSelected();
            boolean bird = btnBird.isSelected();
            if (!cage) {
                elementType = ali ? ElementType.ALI_FRONT : ElementType.BIRD;
            } else {
                if (!ali && !bird) {
                    elementType = ElementType.CAGE;
                } else {
                    elementType = ali ? ElementType.CAGE_ALI_FRONT : ElementType.CAGE_BIRD;
                }
            }
        }
        log.debug("elementType={}", elementType);
    }

    private void createUIComponents() {
        root = this;
    }

    /**
     * Method generated by IntelliJ IDEA GUI Designer
     * >>> IMPORTANT!! <<<
     * DO NOT edit this method OR call it in your code!
     *
     * @noinspection ALL
     */
    private void $$$setupUI$$$() {
        createUIComponents();
        root.setLayout(new GridBagLayout());
        root.setBorder(BorderFactory.createTitledBorder(BorderFactory.createLoweredBevelBorder(), null));
        btnWall = new ImageButton();
        btnWall.setIcon(new ImageIcon(getClass().getResource("/img/map/wall.gif")));
        btnWall.setToolTipText("围墙");
        btnWall.setUnitedIconHeight(3.0f);
        btnWall.setUnitedIconWidth(3.0f);
        GridBagConstraints gbc;
        gbc = new GridBagConstraints();
        gbc.gridx = 2;
        gbc.gridy = 0;
        gbc.weighty = 1.0;
        gbc.fill = GridBagConstraints.BOTH;
        root.add(btnWall, gbc);
        btnAli = new ImageButton();
        btnAli.setIcon(new ImageIcon(getClass().getResource("/img/map/ali_front.gif")));
        btnAli.setToolTipText("阿狸");
        btnAli.setUnitedIconHeight(3.0f);
        btnAli.setUnitedIconWidth(3.0f);
        gbc = new GridBagConstraints();
        gbc.gridx = 4;
        gbc.gridy = 0;
        gbc.weighty = 1.0;
        gbc.fill = GridBagConstraints.BOTH;
        root.add(btnAli, gbc);
        btnBird = new ImageButton();
        btnBird.setIcon(new ImageIcon(getClass().getResource("/img/map/bird.gif")));
        btnBird.setToolTipText("小鸟");
        btnBird.setUnitedIconHeight(3.0f);
        btnBird.setUnitedIconWidth(3.0f);
        gbc = new GridBagConstraints();
        gbc.gridx = 5;
        gbc.gridy = 0;
        gbc.weighty = 1.0;
        gbc.fill = GridBagConstraints.BOTH;
        root.add(btnBird, gbc);
        final JLabel label1 = new JLabel();
        label1.setText("<html>\n地图<br>\n元素\n</html>");
        gbc = new GridBagConstraints();
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.anchor = GridBagConstraints.WEST;
        gbc.insets = new Insets(0, 5, 0, 5);
        root.add(label1, gbc);
        btnCage = new ImageButton();
        btnCage.setIcon(new ImageIcon(getClass().getResource("/img/map/cage.gif")));
        btnCage.setToolTipText("笼子");
        btnCage.setUnitedIconHeight(3.0f);
        btnCage.setUnitedIconWidth(3.0f);
        gbc = new GridBagConstraints();
        gbc.gridx = 3;
        gbc.gridy = 0;
        gbc.weighty = 1.0;
        gbc.fill = GridBagConstraints.BOTH;
        root.add(btnCage, gbc);
        btnEmpty = new ImageButton();
        btnEmpty.setIcon(new ImageIcon(getClass().getResource("/img/mapDesigner/empty.gif")));
        btnEmpty.setToolTipText("空白");
        btnEmpty.setUnitedIconHeight(3.0f);
        btnEmpty.setUnitedIconWidth(3.0f);
        gbc = new GridBagConstraints();
        gbc.gridx = 1;
        gbc.gridy = 0;
        gbc.weighty = 1.0;
        gbc.fill = GridBagConstraints.BOTH;
        root.add(btnEmpty, gbc);
    }

    /**
     * @noinspection ALL
     */
    public JComponent $$$getRootComponent$$$() { return root; }

}
